﻿using System;
using System.Collections.Generic;
using NHibData.Repository;
using NHibernate;

namespace NHibData.SessionManager
{
    public class DictionarySessionManager : INHibenraneSessionManager
    {        
        private readonly IDictionary<string, ISession> _dictionary = new Dictionary<string, ISession>();

        public ISession NHibernateSession
        {
            get
            {
                if(_dictionary.ContainsKey(NHibernateHelper.DefaultNHibernateConfigName))
                    return _dictionary[NHibernateHelper.DefaultNHibernateConfigName];
                return null;
            }
            set
            {
                _dictionary[NHibernateHelper.DefaultNHibernateConfigName] = value;
            }
        }

        public ISession GetSessionByKey(string key)
        {
            if(string.IsNullOrEmpty(key))
                throw new ArgumentNullException("key");

            if (_dictionary.ContainsKey(key))
                return _dictionary[key];
            return null;
        }

        public void SetSessionByKey(string key, ISession session)
        {
            if (string.IsNullOrEmpty(key))
                throw new ArgumentNullException("key");

            _dictionary[key] = session;
        }
    }
}